﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;
using System.Media;

namespace JustAMinute
{
    
      public class Engine
      {
           private List<Card> listOfCards;
           private string themeName;
           private int cardsCount;
           private int time;
           private int points;
           private string levelName;
       
  
           public int Points
        {
            get
            {
                return points;
            }
        }
           public string LevelName
        {
            get
            {
                return levelName;
            }
        }
           public int CardsCount
        {
            get
            {
                return cardsCount;
            }
        }
           public List<Card> ListOfCards
           {
               get
               {
                   return listOfCards;
               }
           }

           public Engine() { }
           public Engine(Level level, Form form)
        {
            this.cardsCount = level.CardsCount;
            this.time = level.Time;
            this.themeName = level.ThemeName;
            this.levelName = level.Name;
            listOfCards = new List<Card>(this.cardsCount);
            points = 0;

        }
           public void CreateCards()
        {
            
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            directory = Path.Combine(directory, @"DataStore\Images");
            directory = Path.Combine(directory, themeName);

            string fullBackName = Path.Combine(directory, "back.png");
           
            Image back = Image.FromFile(fullBackName);

            Image[] faceImagesArray = new Image[cardsCount / 2];

            for(int i = 0; i < faceImagesArray.Length; i++ )
            {
                faceImagesArray[i] = Image.FromFile(Path.Combine(directory, i + ".png"));
            }

   
            Card[] cards = new Card[cardsCount];

            int j= -1;
            for (int i = 0; i < cards.Length; i++ )
            {
                if (i % 2 == 0) j++;
                cards[i] = new Card(faceImagesArray[j], back);
            }

            for (int i = 0; i < cards.Length; i++ )
            {
                listOfCards.Add(cards[i]);
            }

            RandomSort();

        }
           public bool CheckIsPair(List<int> temporaryIndexes, int currentNumberOfCards)
           {
               
               if (currentNumberOfCards == 2)
               {
                       if (listOfCards[temporaryIndexes[0]].Face == listOfCards[temporaryIndexes[1]].Face)
                       {
                           points += listOfCards[temporaryIndexes[0]].Value + listOfCards[temporaryIndexes[1]].Value;
                           cardsCount -= 2;
                           return true;
                       }


                       return false;
                               
               }

               return false;

               
           
           }         
           private void RandomSort()
           {
               Card tmp;
               int index;
               Random random = new Random();
               for (int i = 0; i < listOfCards.Count; i++)
               {
                   for (int z = 0; z < listOfCards.Count; z++)
                   {
                       tmp = listOfCards[z];
                       index = random.Next(0, listOfCards.Count);
                       listOfCards[z] = listOfCards[index];
                       listOfCards[index] = tmp;

                   }
               }
           }
      }
}
